package com.samsung.spen.sshare.explorer.util;

import java.util.ArrayList;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.samsung.spen.sshare.R;
import com.samsung.spen.sshare.explorer.choosefiles.FilesSelectedActivity;
import com.samsung.spen.sshare.explorer.viewholders.ViewHolderSelected;

public class FilesSelectedAdapter extends ArrayAdapter<FileSelected> implements
		Runnable {

	Context context;
	int viewResource;
	ArrayList<FileSelected> values;

	Handler mHandler; // for update UI

	Thread bindImage;
	
	ArrayList<ViewHolderSelected> listHolder = new ArrayList<ViewHolderSelected>();

	public FilesSelectedAdapter(Context context, Handler handler, int resource,
			ArrayList<FileSelected> values) {
		super(context, resource, values);

		this.context = context;
		this.viewResource = resource;
		this.values = values;
		this.mHandler = handler;

		bindImage = new Thread(this);
	}

	@Override
	public FileSelected getItem(int position) {
		return values.get(position);
	}

	@Override
	public int getPosition(FileSelected item) {

		for (int i = 0; i < values.size(); ++i) {

			if (values.get(i) == item) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = convertView;

		if (view == null) {

			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

			view = inflater.inflate(this.viewResource, parent, false);
			ViewHolderSelected viewHolder = new ViewHolderSelected();

			viewHolder.image = (ImageView) view.findViewById(R.id.image);
			viewHolder.title = (TextView) view.findViewById(R.id.title);
			viewHolder.checkBox = (CheckBox) view.findViewById(R.id.cbSelect);

			view.setTag(viewHolder);
			listHolder.add(viewHolder);
		}

		ViewHolderSelected holder = (ViewHolderSelected) view.getTag();

		final FileSelected data = values.get(position);
		holder.setFile(data);

		String title = data.getFile().getName();
		holder.title.setText(title);
		
		//data.setSelect(true);
		holder.checkBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {

//						if (isChecked) {
//							data.setSelect(isChecked);
//
//						} else {
//							data.setSelect(false);
//						}
						//data.setSelect(isChecked);
						
						data.setSelect(isChecked);
						SConfig.FLAG_SELECT_FILE_TO_SEND_ALL = false;

						int temp = 0;
						for (int i = 0; i < values.size(); i++) {
							if (values.get(i).isSelect()) {
								temp++;
							}
						}
						
					FilesSelectedActivity.textView.setText("Num: " + temp);
					
					}
				});

		// holder.image.setImageResource(R.drawable.file_hidden);

		int index = title.lastIndexOf(".");
		String extStr = title.substring(index + 1);
		int idResource = FileUtil.getInstance().getReourceFile(extStr);
		holder.image.setImageResource(idResource);

		if (!bindImage.isAlive())
			bindImage.start();

		if (SConfig.FLAG_SELECT_FILE_TO_SEND_ALL) {

			for (FileSelected iterable_element : values) {
				iterable_element.setSelect(SConfig.FLAG_SELECT_FILE_TO_SEND_ALL);
			}
		}

		return view;
	}

	public void setImage(final ViewHolderSelected holder,
			final FileSelected file, final int idResource) {

		if (idResource == R.drawable.exp_file_image) {

			if (mHandler != null) {

				mHandler.post(new Runnable() {

					public void run() {
						try {
							holder.image.setImageURI(Uri.fromFile(file
									.getFile()));
						} catch (OutOfMemoryError e) {
							holder.image.setImageResource(idResource);
						}
					}
				});
			}
		}
	}

	public void run() {

		while (true) {

			for (ViewHolderSelected holder : listHolder) {

				FileSelected file = holder.getFile();
				int index = file.getFile().getName().lastIndexOf(".");
				String extStr = file.getFile().getName().substring(index + 1);
				int idResource = FileUtil.getInstance().getReourceFile(extStr);
				setImage(holder, holder.getFile(), idResource);

				try {
					Thread.sleep(0);
				} catch (InterruptedException e) {
				}

			}

			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}

			try {

				Thread.sleep(1500);
			} catch (InterruptedException e) {
			}

		}
	}

//	class ViewHolderSelected {
//
//		ImageView image;
//		TextView title;
//		CheckBox checkBox;
//
//		private FileSelected file;
//
//		public FileSelected getFile() {
//			return file;
//		}
//
//		public void setFile(FileSelected file) {
//			this.file = file;
//		}
//
//	}

}
